<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="isLoading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <!-- <span>江苏传智播客教育科技股份有限公司</span> -->
            <span>{{ company.name }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex">
              <!-- 两个内容 -->
              <el-col :span="12">负责人</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDepts(company)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 用了一个行列布局 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown @command="handleClickqDepts($event, data)">
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                        <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                        <el-dropdown-item command="del">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- sync：表示父组件把组件传给子组件的值，放权给子组件去修改 -->
      <addDepts ref="addDepts" :show-dialog.sync="showDialog" :node-data="nodeData" :depart-list="departList" @add-success="getDeparmentsList" @closeDialogFn="closeDialog" />
    </div>
  </div>
</template>

<script>
import { getDeparmentsListApi, delDepartmentsApi } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import addDepts from './components/add-depts.vue'
export default {
  name: 'Departments',
  components: {
    addDepts
  },
  data() {
    return {
      company: {
        id: '',
        name: '',
        manager: '负责人',
        children: []
      },
      departs: [],
      defaultProps: {
        label: 'name'
        // children: 'children'
      },
      showDialog: false,
      nodeData: {},
      departList: [],
      isLoading: false
    }
  },
  created() {
    this.getDeparmentsList()
  },
  methods: {
    async getDeparmentsList() {
      this.isLoading = true
      const { data } = await getDeparmentsListApi()
      this.company.name = data.companyName
      this.departList = data.depts
      // console.log(data)
      this.departs = tranListToTreeData(data.depts, '') // 需要将其转换成树形结构
      this.company.children = this.departs
      this.isLoading = false
    },
    handleClickqDepts(cmd, data) {
      // console.log(cmd)
      // console.log(data)
      if (cmd === 'add') {
        this.addDepts(data)
      }
      if (cmd === 'edit') {
        this.editDepts(data)
      }
      if (cmd === 'del') {
        this.delDepts(data.id)
      }
    },

    editDepts(data) {
      this.showDialog = true
      this.nodeData = data
      // this.$refs.addDepts.getDepartmentDetail()
      // this.$refs.addDepts.showTitle = '编辑部门'
      this.$nextTick(() => {
        this.$refs.addDepts.getDepartmentDetail()
      })
    },
    /**
     * 添加功能
     */
    addDepts(data) {
      this.showDialog = true
      // console.log(data)
      this.nodeData = data
      // this.$refs.addDepts.showTitle = '添加部门'
      // this.$refs.addDepts.getUserSimpleList()
    },
    closeDialog() {
      this.showDialog = false
    },
    // 删除功能
    delDepts(id) {
      this.$confirm('您确定要删除吗？该操作是不可逆的', '温馨提示').then(async() => {
        await delDepartmentsApi(id)
        this.$message.success('删除成功')
        this.getDeparmentsList()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
